<!--车辆管理页-->
<template>
  <div class="view">
    <span class="view-title">车辆管理</span>
    <el-button class="title-right-button" @click="handleCreate" type="primary">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog class="dialog" :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="vehicle.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="vehicle.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="vehicle.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="vehicle.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="vehicle.displacement">
              <el-option label="0(新能源)" value="0"></el-option>
              <el-option label="1.0~2.0L" value="1"></el-option>
              <el-option label="2.0~3.0L" value="2"></el-option>
              <el-option label="3.0~4.0L" value="3"></el-option>
              <el-option label="4L以上" value="4"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="vehicle.type">
              <el-option label="轿车" value="10"></el-option>
              <el-option label="货车" value="20"></el-option>
              <el-option label="客车" value="30"></el-option>
              <el-option label="挂车" value="40"></el-option>
              <el-option label="SUV" value="50"></el-option>
              <el-option label="MPV" value="60"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="vehicle.color">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="vehicle.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="vehicle.buyTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="vehicle.regTime"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="vehicle.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="vehicle.batteryType">
              <el-option label="铅酸蓄电池" value="10"></el-option>
              <el-option label="镍氢电池" value="20"></el-option>
              <el-option label="钠硫电池" value="30"></el-option>
              <el-option label="二次锂电池" value="40"></el-option>
              <el-option label="空气电池" value="50"></el-option>
              <el-option label="三元锂电池" value="60"></el-option>
              <el-option label="碱性燃料电池" value="70"></el-option>
              <el-option label="磷酸铁锂电池" value="80"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button @click="saveVehicle" type="primary">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card class="search-card">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.brand"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.license"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card class="table-card">
    <el-table :data="vehicleArr" height="600">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="handleUpdate(scope.row.id)" size="small">编辑</el-button>
          <el-button type="danger" @click="openDeleteConfirmBox(scope.row.id)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage, ElMessageBox} from "element-plus";
import qs from "qs";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');
//定义对象用来保存车辆搜索条件
const searchVehicleForm = ref({brand: '', license: ''});
//定义数组用来保存车辆表格数据
const vehicleArr = ref([]);
//定义加载车辆的方法
const loadVehicle = () => {
  let data = qs.stringify(searchVehicleForm.value)
  axios.get(BASE_URL + '/v1/vehicle/select?' + data)
      .then((response) => {
        if (response.data.code == 2000) {
          vehicleArr.value = response.data.data;
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//页面加载时执行
onMounted(() => {
  loadVehicle();
});
//定义搜索卡片重置时执行
const resetSearch = () => {
  searchVehicleForm.value = {};
  // loadVehicle();
}
//取消新建车辆
const cancel = () => {
  dialogVisible.value = false;
};
//车辆信息
const vehicle = ref({
  brand: '',
  license: '',
  model: '',
  code: '',
  displacement: '',
  type: '',
  color: '',
  kilometers: '',
  buyTime: '',
  regTime: '',
  price: '',
  batteryType: ''
});
//新建车辆按钮
const handleCreate = () => {
  //确保每次新增时的标题,并且弹窗表单中没有数据
  vehicle.value = {};
  dialogTitle.value = '新建车辆';
  dialogVisible.value = true;
}
//保存车辆信息
const saveVehicle = () => {
  let data = qs.stringify(vehicle.value);
  console.log(vehicle.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/vehicle/insert', data)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('操作成功!');
          window.setTimeout(() => {
            dialogVisible.value = false;
            loadVehicle();
          }, 1000)
        } else {
          ElMessage.error('车牌号与车辆识别码不允许重复!');
        }
      });
}
//对车辆状态显示内容进行文字转换
const vehicleStatusFormatter = (row, column, cellValue, index) => {
  if (cellValue == 1) {
    cellValue = '空闲';
  } else if (cellValue == 2) {
    cellValue = '占用';
  }
  return cellValue;
}
//对车辆类型显示内容进行文字转换
const vehicleTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '轿车';
  } else if (cellValue == 20) {
    cellValue = '货车';
  } else if (cellValue == 30) {
    cellValue = '客车';
  } else if (cellValue == 40) {
    cellValue = '挂车';
  } else if (cellValue == 50) {
    cellValue = 'SUV';
  } else if (cellValue == 60) {
    cellValue = 'MPV';
  }
  return cellValue;
}
//对电池类型显示内容进行文字转换
const batteryTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '铅酸蓄电池';
  } else if (cellValue == 20) {
    cellValue = '镍氢电池';
  } else if (cellValue == 30) {
    cellValue = '钠硫电池';
  } else if (cellValue == 40) {
    cellValue = '二次锂电池';
  } else if (cellValue == 50) {
    cellValue = '空气电池';
  } else if (cellValue == 60) {
    cellValue = '三元锂电池';
  } else if (cellValue == 70) {
    cellValue = '碱性燃料电池'
  } else if (cellValue == 80) {
    cellValue = '磷酸铁锂电池'
  }
  return cellValue;
}
//处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('表单内容将不会保存,确认离开吗?')) {
    dialogVisible.value = false;
    vehicle.value = {};
  }
}
//修改之前先查一次
const handleUpdate = (id) => {
  vehicle.value = {};
  dialogTitle.value = '编辑车辆';
  dialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?id=' + id)
      .then((response) => {
        if (response.data.code == 2000) {
          vehicle.value = response.data.data[0];
          console.log(vehicle.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//删除车辆
const handleDelete = (vehicleId) => {
  axios.delete(BASE_URL + '/v1/vehicle/delete/' + vehicleId)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          });
          loadVehicle();
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//点击删除弹出确认提示框
const openDeleteConfirmBox = (vehicleId) => {
  ElMessageBox.confirm(
      '删除后不可恢复,确认删除吗?',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        handleDelete(vehicleId);
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '操作取消',
        })
      })
}
</script>

<style scoped>
.view {
  height: 4vh;
  padding: 5px 20px;
  background-color: #fff;
}

.view-title {
  line-height: 40px;
  font-size: 20px;
  font-weight: bold;
}

.title-right-button {
  float: right;
  margin-top: 3px;
}

.dialog {
  width: 1000px;
  padding: 40px;
}

.search-card {
  margin: 20px;
  height: 70px;
}

.table-card {
  margin: 20px;
}
</style>